<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>监测点频谱建模</title>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/demo/demo.css">
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/jquery.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/Highcharts-5.0.14/code/highcharts.js"></script>
	<script src="${pageContext.request.contextPath}/static/Highcharts-5.0.14/code/modules/exporting.js"></script>
	<script src="https://img.hcharts.cn/highcharts-plugins/highcharts-zh_CN.js"></script>
</head>
<body>
    <div>
    &nbsp;&nbsp;当前干扰分析状态：<input type="text" id="currentStatus" name="currentStatus" class="easyui-validatebox" values="" style="text-align:center;width: 300px" />
    </div>
    <p>
    <div>
    &nbsp;建模类型：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 	<input class="easyui-combobox" id="ModelType" name="ModelType" data-options="panelHeight:'auto',valueField:'lable',textField:'value',
					    data:[{
					                lable:'1',
					                value:'重新替换模型'
					            },
					            {
					                lable:'0',
					                value:'增加建模时间'
					            },
					            {
					                lable:'-1',
					                value:''
					            }]" />
    <p>
    &nbsp;选择小区：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 	<input class="easyui-combobox" id="frontId" name="FrontId" style="width:200px" data-options="panelHeight:'auto',valueField:'id',textField:'name',url:'${pageContext.request.contextPath}/interferencequery/frontIdBox.do'"/>
    <p>
    <a href="javascript:gettime()" class="easyui-linkbutton" >确定选择</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a href="javascript:refresh()" class="easyui-linkbutton" >重新选择</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <p>&nbsp;可选时间范围：&nbsp;<input type="text" id="begintime" size="20" class="easyui-textbox" valueField="" textField="" style="width:150px"/>&nbsp;至&nbsp;
	<input type="text" id="endtime" size="20" class="easyui-textbox" style="width:150px" />
    <p>&nbsp;选择时间范围：&nbsp;<input type="text" name="SelectBeginTime" id="SelectBeginTime" size="20" class="easyui-datetimebox" style="width:150px"/>&nbsp;至&nbsp;
	<input type="text" id="SelectEndTime" name="SelectEndTime" size="20" class="easyui-datetimebox"  style="width:150px" /><p>
	&nbsp;建模状态：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" id="Status" size="20" class="easyui-textbox" valueField="" textField="" style="width:250px"/>&nbsp;&nbsp;
	<p><a href="javascript:CreateModel()" class="easyui-linkbutton" >开始建模</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="javascript:CancelModel()" class="easyui-linkbutton" >取消建模</a>
	<p><a href="javascript:saveBusyUp()" class="easyui-linkbutton" >保存上行忙时模型</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="javascript:saveLeisureUp()" class="easyui-linkbutton" >保存上行闲时模型</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="javascript:saveBusyDown()" class="easyui-linkbutton" >保存下行忙时模型</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="javascript:saveLeisureDown()" class="easyui-linkbutton" >保存下行闲时模型</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	</div>
	<p>
	<div id="container" style="float:left;width:calc(50%);height:500px"></div>
	<div id="container2" style="float:left;width:calc(50%);height:500px"></div>
<script type="text/javascript">
  var modeltype=0; //0表示正常,1表示重新建模,2表示增加建模时间
  var TimeBegin; //最小可选的时间
  var TimeEnd;   //最大可选的时间
  var FrontId;   //所选择的前置机
  var type;      //选择的建模类型
  var timer;
  var oldBusyModelUp=new Array();
  var oldLeisureModelUp=new Array();
  var oldBusyModelDown=new Array();
  var oldLeisureModelDown=new Array();
  var newBusyModelUp=new Array();
  var newLeisureModelUp=new Array();
  var newBusyModelDown=new Array();
  var newLeisureModelDown=new Array();
  var direction;//0表示上行,1表示下行
  var selectTime;//0表示闲时,1表示忙时
  
  setInterval(function(){
	  getStatus(); 
  },1000);
  function getStatus() {
	  var url = "${pageContext.request.contextPath}/interferencequery/Status.do";  
		var data = {type:1};
		$.ajax({   type : "get",   async : true, //同步请求   
			url : url,   
			data : data,      
			dataType:"json",
			success:function(dates){
				//转为JSON对象
			var dataObj=JSON.stringify(dates); 
			var val = JSON.parse(dataObj);
			//var dataObj = {"total":28,"rows":[{"frontProcessorID":"111","recordID":"Koi","downGain":10.00,"upGain":"P","timeStamp":36.50,"timeStampStr":"Large","cellInfo":"EST-1"}]};
		   // $("Status").html(val.status);
			//	$("#dg").datagrid("loadData",dataObj);
			if(val.status>0){
				$('#currentStatus').val("干扰分析正在进行中,请等干扰分析停止再开始建模！！");
			} else{
				$('#currentStatus').val("本时段干扰分析已完成,可以开始建模！！");
			} 		
			} 
			});   
  }
  function saveBusyUp(){
	  direction=0;
	  selectTime=1;
	  $.post("${pageContext.request.contextPath}/interferencequery/replacebsmodel.do",{frontId:FrontId,direction:direction,selectTime:selectTime},function(result){
		    if(result.success){
		    	$("#Status").val("忙时上行模型替换结束！");
		    }
		    else{
		    	$.messager.alert("系统提示","当前数据库中没有该前置机的模型，忙时上行模型替换出错！");
		    }
				},"json");
  }
  function saveLeisureUp(){
	  direction=0;
	  selectTime=0;
	  $.post("${pageContext.request.contextPath}/interferencequery/replacebsmodel.do",{frontId:FrontId,direction:direction,selectTime:selectTime},function(result){
		    if(result.success){
		    	$("#Status").val("闲时上行模型替换结束！");
		    }
		    else{
		    	$.messager.alert("系统提示","当前数据库中没有该前置机的模型，闲时上行模型替换出错！");
		    }
				},"json");
  }
  function saveBusyDown(){
	  direction=1;
	  selectTime=1;
	  $.post("${pageContext.request.contextPath}/interferencequery/replacebsmodel.do",{frontId:FrontId,direction:direction,selectTime:selectTime},function(result){
		    if(result.success){
		    	$("#Status").val("忙时下行模型替换结束！");
		    }
		    else{
		    	$.messager.alert("系统提示","当前数据库中没有该前置机的模型，忙时下行模型替换出错！");
		    }
				},"json");
  }
  function saveLeisureDown(){
	  direction=1;
	  selectTime=0;
	  $.post("${pageContext.request.contextPath}/interferencequery/replacebsmodel.do",{frontId:FrontId,direction:direction,selectTime:selectTime},function(result){
		    if(result.success){
		    	$("#Status").val("闲时下行模型替换结束！");
		    }
		    else{
		    	$.messager.alert("系统提示","当前数据库中没有该前置机的模型，闲时下行模型替换出错！");
		    }
				},"json");
  }
  
  function gettime(){
	  type=$("#ModelType").combobox("getText");
	  FrontId=$("#frontId").combobox("getText");
	  
	  
	  if(!type || type.lenght < 1 || type == ''){
		  $.messager.alert("系统提示","请先选择建模类型！");
		  return;
	  }
	  if(!FrontId || FrontId.lenght < 1 || FrontId == ''){
		  $.messager.alert("系统提示","请先选择前置机！");
		  return;
	  }
	  $.post("${pageContext.request.contextPath}/interferencequery/getSelectTime.do",{frontId:FrontId},function(result){
		    TimeBegin=result.startTime;
		    TimeEnd=result.endTime;
		    $("#begintime").val(TimeBegin);
		    $("#endtime").val(TimeEnd);
		    $("#SelectBeginTime").datebox('setValue', TimeBegin);
			$("#SelectEndTime").datebox('setValue', TimeEnd); 
				},"json");
	  /* 显示当前底噪 */
	  chartBaseModel();
  }
  function refresh(){
	  location.reload();
  }
  function chartBaseModel(){
	  var series_bsmodelbusyUp = chart1.series[0]; 
      var series_bsmodelleisureUp = chart1.series[1];
      var series_bsmodelbusyDown = chart2.series[0]; 
      var series_bsmodelleisureDown = chart2.series[1];
      // set up the updating of the chart each second
      var tmp=998;
      var sum=0; 
      var fs=new Array();
      for(var j=998;j<=1020;j=j+1/21){
      	if(sum==21){
      		chart1.xAxis[0].categories.push(tmp+1);
      		chart2.xAxis[0].categories.push(tmp+1);
      		tmp=tmp+1;
      		sum=0;
      	}
      	else{
      		chart1.xAxis[0].categories.push(j);
      		chart2.xAxis[0].categories.push(j);
      	}    	
      	sum=sum+1;
      } 
 
  	  $.post("${pageContext.request.contextPath}/interferencequery/bsmodellist.do",{frontId:FrontId,direction:"0"},function(result){
  		var r=result.rows;
  		for(var i=0;i<463;i++)
  		{
  			if(i<11||i>451)
  			{
  				oldBusyModelUp[i]=null;
  				oldLeisureModelUp[i]=null;
  			}
  			else{
  				//if (r != null && r.lenght > 0){
  					oldBusyModelUp[i]=parseFloat(r[i-11].busyFloor);
  					oldLeisureModelUp[i]=parseFloat(r[i-11].leisureFloor);
  				//}
  			}
  		}
  		var tempOldBusyUp = new Array();
  		var tempLeisureBusyUp = new Array();
			for (var i = 0; i < 463; i++){
				tempOldBusyUp[i] = oldBusyModelUp[i];
				tempLeisureBusyUp[i] = oldLeisureModelUp[i];
			}
		series_bsmodelbusyUp.setData(tempOldBusyUp);
  		series_bsmodelleisureUp.setData(tempLeisureBusyUp);
  		
 	   if(!result.success){
 			$.messager.alert("系统提示","获取参数失败！");
 		}
		},"json"); 
  	  $.post("${pageContext.request.contextPath}/interferencequery/bsmodellist.do",{frontId:FrontId,direction:"1"},function(result){
  		  
  		var r=result.rows;
  		
  		for(var i=0;i<463;i++) {
  			if(i<11||i>451) {
  				oldBusyModelDown[i]=null;
  				oldLeisureModelDown[i]=null;
  			} else {
  				//if (r != null && r.lenght > 0){
  					oldBusyModelDown[i]=parseFloat(r[i-11].busyFloor);
  					oldLeisureModelDown[i]=parseFloat(r[i-11].leisureFloor);
  				//}
  			}
  		}
  		var tempOldBusyDown = new Array();
  		var tempLeisureBusyDown = new Array();
			for (var i = 0; i < 463; i++){
				tempOldBusyDown[i] = oldBusyModelDown[i];
				tempLeisureBusyDown[i] = oldLeisureModelDown[i];
			}
  		series_bsmodelbusyDown.setData(tempOldBusyDown);
		series_bsmodelleisureDown.setData(tempLeisureBusyDown);
	 	   if(!result.success){
	 			$.messager.alert("系统提示","获取参数失败！");
	 		}
		},"json"); 
  }
  function CreateModel(){
	  var BeginTime=$("#SelectBeginTime").datebox("getValue");
  	  var EndTime=$("#SelectEndTime").datebox("getValue");
  	  if (!BeginTime || BeginTime.lenght < 1 || !EndTime || EndTime.lenght < 1 || !TimeBegin || TimeBegin.lenght < 1 || !TimeEnd || TimeEnd.lenght < 1) {
  			$.messager.alert("系统提示","请选择时间！");
		   	return;
  	  }
  	  var earlytimelong = new Date(TimeBegin.replace(new RegExp("-","gm"),"/")).getTime();
  	  var endtimelong = new Date(TimeEnd.replace(new RegExp("-","gm"),"/")).getTime();
  	  var startTimeLong = new Date(BeginTime.replace(new RegExp("-","gm"),"/")).getTime();
	  var endTimeLong = new Date(EndTime.replace(new RegExp("-","gm"),"/")).getTime();
	  if(startTimeLong-endTimeLong>=0){
			$.messager.alert("系统提示","所选的开始时间"+TimeBegin+"不能大于结束时间"+TimeEnd+"！");
			return;
	  }
	  if((startTimeLong<earlytimelong||startTimeLong>endtimelong)||(endTimeLong<earlytimelong||endTimeLong>endtimelong)){
			$.messager.alert("系统提示","所选时间不能超出可选时间！");
			return;
	  }
	  
	  if(type=='重新替换模型'&&(endTimeLong-startTimeLong<24*60*60*1000||endTimeLong-startTimeLong>3*24*60*60*1000)){
		   $.messager.alert("系统提示","所选时间范围需要大于1天且小于3天");
		   return;
	  }
	  if (!FrontId || FrontId.lenght < 1 || FrontId != $("#frontId").combobox("getText") || !type || type.lenght < 1 || type!=$("#ModelType").combobox("getText")) {
		  $.messager.alert("系统提示","请重新确定选择！");
		   return;
	  }
	  
	  if(type=='重新替换模型'){
		  ModelType=1;
	  } else if(type=='增加建模时间'){
		  ModelType=2;
	  }
	  
	  $.post("${pageContext.request.contextPath}/interferencequery/createModel.do",{frontId:FrontId,modelType:ModelType,modelBeginTime:startTimeLong,modelEndTime:endTimeLong},function(result){
		    if(!result.success){
		    	$.messager.alert("系统提示","建模失败，请重新尝试！");
		    }
		},"json");
	  timer=setInterval(function(){
		  getmodelStatus(); 
	  },1000);
  }
  
  function CancelModel(){
	  $.post("${pageContext.request.contextPath}/interferencequery/cancelModel.do",function(result){
		    if(result.success){
		    	$("#Status").val("成功取消建模!");
		    	$.messager.alert("系统提示","成功取消建模");
		    }
				},"json");
  }
  
  function getmodelStatus() {
	    var url = "${pageContext.request.contextPath}/interferencequery/modelStatus.do";  
		var data = {type:1};
		$.ajax({   type : "get",   async : true, //同步请求   
			url : url,   
			data : data,      
			dataType:"json",
			success:function(dates){
				//转为JSON对象
			var dataObj=JSON.stringify(dates); 
			var val = JSON.parse(dataObj);
			if(val.ModelStatus>0){
				$("#Status").val("建模中……");
			}
			else{
				$("#Status").val("建模已完成,请确定是否保存建模结果！");
				chartNewBaseModel();
				clearInterval(timer);
			} 		
			},   
			error: function() {
				alert("获取数据失败，请稍后再试！");
			}  
			});   
  }
  
  function chartNewBaseModel(){
	  var series_bsmodelbusyUp = chart1.series[0]; 
      var series_bsmodelleisureUp = chart1.series[1];
      var series_bsmodelbusyUpNew = chart1.series[2]; 
      var series_bsmodelleisureUpNew = chart1.series[3];
      var series_bsmodelbusyDown = chart2.series[0]; 
      var series_bsmodelleisureDown = chart2.series[1];
      var series_bsmodelbusyDownNew = chart2.series[2]; 
      var series_bsmodelleisureDownNew = chart2.series[3];
      // set up the updating of the chart each second
      var tmp=998;
      var sum=0; 
      var fs=new Array();
      for(var j=998;j<=1020;j=j+1/21){
      	if(sum==21){
      		chart1.xAxis[0].categories.push(tmp+1);
      		chart2.xAxis[0].categories.push(tmp+1);
      		tmp=tmp+1;
      		sum=0;
      	}
      	else{
      		chart1.xAxis[0].categories.push(j);
      		chart2.xAxis[0].categories.push(j);
      	}    	
      	sum=sum+1;
      } 
 
  	  $.post("${pageContext.request.contextPath}/interferencequery/bsmodeltransitionlist.do",{frontId:FrontId,direction:"0"},function(result){
  		var r=result.rows;
  		for(var i=0;i<463;i++)
  		{
  			if(i<11||i>451)
  			{
  				newBusyModelUp[i]=null;
  				newLeisureModelUp[i]=null;
  			}
  			else{
  				newBusyModelUp[i]=parseFloat(r[i-11].busyFloor);
  				newLeisureModelUp[i]=parseFloat(r[i-11].leisureFloor);
  		
  			}
  		}
  		series_bsmodelbusyUp.setData(oldBusyModelUp);
		series_bsmodelleisureUp.setData(oldLeisureModelUp);
		series_bsmodelbusyUpNew.setData(newBusyModelUp);
		series_bsmodelleisureUpNew.setData(newLeisureModelUp);
 	   if(!result.success){
 			$.messager.alert("系统提示","获取参数失败！");
 		}
		},"json"); 
  	  $.post("${pageContext.request.contextPath}/interferencequery/bsmodeltransitionlist.do",{frontId:FrontId,direction:"1"},function(result){
  		var r=result.rows;
  		for(var i=0;i<463;i++)
  		{
  			if(i<11||i>451)
  			{
  				newBusyModelDown[i]=null;
  				newLeisureModelDown[i]=null;
  				
  			}
  			else{
  				newBusyModelDown[i]=parseFloat(r[i-11].busyFloor);
  				newLeisureModelDown[i]=parseFloat(r[i-11].leisureFloor);
  		
  			}
  		}
  		series_bsmodelbusyDown.setData(oldBusyModelDown);
		series_bsmodelleisureDown.setData(oldLeisureModelDown);
		series_bsmodelbusyDownNew.setData(newBusyModelDown);
		series_bsmodelleisureDownNew.setData(newLeisureModelDown);
	 	   if(!result.success){
	 			$.messager.alert("系统提示","获取参数失败！");
	 		}
		},"json"); 
  	  
  }
  
  /* 显示当前底噪 */
  Highcharts.setOptions({
	    global: {
	        useUTC: false
	    }
	});
		
	Highcharts.setOptions({
	    // 所有语言文字相关配置都设置在 lang 里
	    lang: {
	        resetZoom: '重置',
	        resetZoomTitle: '重置缩放比例'
	    }
	});
	
	var bsmodelbusy=new Array();
	var bsmodelleisure=new Array();
    var bsmodelbusy2=new Array();   //下行
	var bsmodelleisure2=new Array();  //下行
	var chart1=new Highcharts.chart({
	    	credits:{
			     enabled:false // 禁用版权信息
			},
	        chart: {
	        	renderTo: 'container',   //通过 chart.renderTo 来指定highchart名称
	            type: 'spline',
	            	zoomType: 'xy',
	    	        animation: Highcharts.svg, // don't animate in old IE
	    	        marginRight: 10,
	    	        events: {
	    	            load: function () {
	    	                // set up the updating of the chart each second
	    	            } 
	    	        }
	        },
	        title: {
	            text: '频谱底噪(上行)'
	        },
	        xAxis: {
	        	tickInterval: 21,
	        	allowDecimals:false,
	        	title: {
	                text: '频道号'
	            },
	               categories:[]
	        },
	        yAxis: {
	            title: {
	                text: '频谱值(dBm)'
	            },
	            labels: {
	                formatter: function () {
	                    return this.value + '';
	                }
	            },
	            tickInterval: 2,
	            tickPositions: [-120,-110,-100,-90,-80, -70, -60,-50,-40,-30,-20]
	        },
	        tooltip: {
	            crosshairs: true,
	            shared: true,
	            valueDecimals : 1
	        },
	        plotOptions: {
	            spline: {
	                marker: {
	                    radius: 4,
	                    lineWidth: 0.5
	                }
	            }
	        },
	        Color: ['#7cb5ec', '#434348', '#90ed7d'],
	        exporting: {
		        enabled: true
		    },
	        series: [{
	            name: '原始忙时底噪',
	            marker: {
	            	 enabled: false
	            }, 
	            data: [],
	            color:'#000000',
	            lineWidth: 1
	        },{
	            name: '原始闲时底噪',
	            marker: {
	            	 enabled: false
	            }, 
	            data: [],
	            color:'#0000FF',
	            lineWidth: 1
	        },{
	            name: '新建忙时底噪',
	            marker: {
	            	 enabled: false
	            }, 
	            data: [],
	            color:"#ff0000",
	            lineWidth: 1
	        },{
	            name: '新建闲时底噪',
	            marker: {
	            	 enabled: false
	            }, 
	            data: [],
	            color:"#06ad50",
	            lineWidth: 1
	        }]
	    });
	
	var chart2=new Highcharts.chart({
	    	credits:{
			     enabled:false // 禁用版权信息
			},
	        chart: {
	        	renderTo: 'container2',   //通过 chart.renderTo 来指定highchart名称
	                type: 'spline',
	            	zoomType: 'xy',
	    	        animation: Highcharts.svg, // don't animate in old IE
	    	        marginRight: 10,
	    	        events: {
	    	            load: function () {
	    	               
	    	            } 
	    	        }
	        },
	        title: {
	            text: '频谱底噪(下行)'
	        },
	        xAxis: {
	        	tickInterval: 21,
	        	allowDecimals:false,
	        	title: {
	                text: '信道号'
	            },
	               categories:[]
	        },
	        yAxis: {
	        	pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>',
	            title: {
	                text: '频谱值(dBm)'
	            },
	            labels: {
	                formatter: function () {
	                    return this.value + '';
	                }
	            },
	            tickInterval: 2,
	            tickPositions: [-120,-110,-100,-90,-80, -70, -60,-50,-40,-30,-20]
	        },
	        tooltip: {
	            crosshairs: true,
	            shared: true,
	            valueDecimals : 1
	        },
	        plotOptions: {
	            spline: {
	                marker: {
	                    radius: 4,
	                    lineColor:['#7cb5ec', '#434348', '#90ed7d'],
	                    lineWidth: 0.5
	                }
	            }
	        },
	        exporting: {
		        enabled: true
		    },
	        series: [{
	            name: '原始忙时底噪',
	            marker: {
	            	 enabled: false
	            }, 
	            data: [],
	            color:'#000000',
	            lineWidth: 1
	            
	        },{
	            name: '原始闲时底噪',
	            marker: {
	            	 enabled: false
	            }, 
	            data: [],
	            color:'#0000FF',
	            lineWidth: 1
	        },{
	            name: '新建忙时底噪',
	            marker: {
	            	 enabled: false
	            }, 
	            data: [],
	            color:"#ff0000",
	            lineWidth: 1
	        },{
	            name: '新建闲时底噪',
	            marker: {
	            	 enabled: false
	            }, 
	            data: [],
	            color:"#06ad50",
	            lineWidth: 1
	        }]
	    });

 
</script>
</body>
</html>